Today I'm going to introduce a programming construct called a Table.
So when we want to store a bunch of things under one variable, we use a table.
Tables hold a collection of things, whether they're numbers or parts.
Zombies, players, you can see how this is going to be very handy.
This is a fresh world right here, only to only to show you tables.
I'm going to go to workspace, hit the plus, add a script, and this script I'm going to call table
intro.
Cool beans.
We'll get rid of that print statement.
We don't need it.
And when I do tables, there are two basic types of tables.
We've got a list type of table, and we've got a key value pair of table, both tables, slightly different
behaviour depending on what you have in the table.
I'll put a comment here, put those two dashes, list type table and then I'll make a variable local.
My list table equals will initialize it with some data.
Use these curly brackets because it's a table.
That's how you initialize a table.
Let's put strings in there.
Let's put strings of monster names.
So I'm going to say zombie and I'll say Ghost and Goblin.
Is that one B?
I think so.
All right.
So there's three elements in here.
One, two, three, three things in my table under one variable, and let's get the other the other
type of table data or the other type of table key value pair data.
Right.
So I'll say a key value pair of table local, my cave table.
I thought that's a good name.
We need curly brackets again because it's still a table and now we're going to come up with key value
pairs.
So what I want to store in here, let's say like player data.
So we'll do like name equals Ralph.
All right.
So this is a string.
So we need we need speech quotes.
Next one I think I'm going to do is XP.
That's a number.
So we don't need speech quotes.
And then the third one, how about title?
What's his title?
Noob.
Right.
3500 seems pretty low.
All right, let's print out the data in the output window to see what we have in there.
So, say, print my list data.
My list table.
Print my cave table.
Cool beans.
Let's go to our view.
Let's open up our output window right here.
With this new annoying icon.
Now, I figure.
Finally figure it out.
All right.
Now, if you have default settings set for your output window, you're not going to see the data in
your tables when you print them out.
We can move this down a little bit for these.
What we need to do is go to these three dots.
Make sure log mode is off default.
It's going to be on.
You're only going to see the table address.
It's not going to be useful off.
Cool.
Let's run it.
There we go.
So long mode off.
You're going to see these brackets with these three dots.
You can open that up.
There you go.
There's your data and it's got numbers, right?
One, two, three.
These are an order.
Zombie ghost goblin.
Let's take a look down here.
Oh.
What happened here?
We got our name.
That's good.
Title and XP switched.
Right.
So it goes to show you that.
And you got to remember this.
The table data with key value pairs order isn't guaranteed.
You'd have to do something to enforce order here.
This is Alexa graphical order.
So it's going by alphabet.
But if I made exp capital it would be up here because capital letters start before lowercase letters
and Alexa graphical ordering.
Anyway, you don't need to know that.
Just know that order is not guaranteed.
Cool.
Let's continue.
All right.
Let's go ahead and close this so we have some more room.
I want to get a particular element in each table and we'll just print it out.
We won't put it in variables or anything.
So print and I won't say list type.
Tables.
Second element equals, well, let's get our list type table.
Now you're going to need these square brackets.
These are square brackets when you're getting an element.
These are curly brackets when you're declaring.
That's a little tricky.
So we want to get let's get ghost.
That's the second element.
And Lua, we start 1 to 3 in like C and Java.
You start with zero.
Here we start with one.
So if we want to ghost two, let's print out one for the key value pair.
Print k v type tables.
Name key.
We'll type out the name.
How do you do that?
I think it's a little more intuitive.
Let's get my key value table.
Dot all of that even gives you even tells you what's available.
I want a name.
There you go.
Let's print that out.
Go to view.
Go to output.
Hit the print.
Let's take a look.
Let's type.
Table.
Second element.
Ghost.
Nice.
Is it Ghost?
It is ghost.
And then here k v type table's name.
That should be a CVV type table's name.
Ralph.
Let's go ahead and fix my typo so that we're not confused.
Key value, right?
That's what you want.
Key value.
All right.
How do we insert an element?
Well, it's not that bad in the list type.
There's many ways to do it.
I'm just going to pick one.
Just pick one.
Right.
This is one of them.
Table insert.
Right.
So this is provided to you by Roblox.
It's a it's a Lua function actually and you could do your table name or this is how you do it and then
put what you want in there.
I'm going to say werewolf.
And that's going to append it at the end of the list.
Right.
So it's going to be here.
Now, you can actually change where you want it to be placed in the list, but we'll do that in other
videos.
I want to keep this basic.
How do you add let's say we want to add level to this table.
Keep in mind, these tables are initialized, doesn't have to have data in them, but they do have to
be initialized.
So if you initialize it with no data like that, it will be fine.
But if you have a nil here, you're going to run into trouble.
All right.
I just did a control Z to put that back.
Let's go and get our let's go ahead and put an insert of value a level the level key in my key value
table.
Dot, look at this level.
Nice too.
And now we want to be able to see our data.
We could get these prints and go down here.
But I also want to show you how to do loops.
Right.
And in in tables we use a key value pair loop.
What's it called?
It is called a for loop in pairs.
Whether you're doing the list type or the key value type, watch for I and V in pairs.
Put your table name in here.
We'll do the list type first.
Do so this if it's a list type table, this is going to be one, two, three, four.
It's going to be the numbers.
This is going to be the values.
Zombie ghost goblin werewolf.
Right.
And operate them out.
Let's do a print statement.
Print I equals I, v equals v.
And this is how convenient this for loop is.
Let's go ahead and copy it.
Control C, control V and then let's put our other table in here.
Just paste it.
My cave table.
What's I going to be?
It's going to be the keys.
It's going to be name.
It's going to be XP.
It's going to be level.
What are the V's going to be?
It's going to be Ralph 3600 or whatever.
That number was 3500.
Nube And then we added level two.
So it's going to print out, let's try it out.
Let's take a look.
View output.
Check it out.
So we have our numbers.
One, two, three, four.
Werewolf was, in fact, appended to the bottom.
Title.
Oh, look at this.
So now it's not even lexical order.
We have title, we have level, which should be first if we're doing some sort of ordering name and
exp.
Remember, key value pairs order is not guaranteed, but we have all of our elements and we printed
through them using the for loop.
Ooh, I got to share one more thing.
I got a comment on this on one of my YouTube videos.
If you are using a list type of of table, it has numbers one, two, three, four use IE pairs.
That's a little more efficient, right?
That's for integer pairs.
You're going to get the exact same thing.
What's going to be crazy?
You know what?
I'm not going to go into it because it's an intro, introduction to tables.
Let's print it out.
Let's just see what we got.
Look at that, exactly the same use in pairs or use pairs.
I don't think the efficiency is going to change much on the level that we're doing.
We're not we're not doing megabytes of data.
So with that, I will see you in the next section when we start building our games and we're going to
do a lot more stuff with tables.